

package com.thxrinfo.stop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.thxrinfo.stop.api.entity.Parking;
import com.thxrinfo.stop.api.vo.ParkingVO;
import com.thxrinfo.stop.service.ParkingService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;


/**
 * 停车场表
 *
 * @author silencezheng
 * @date 2022-12-06 15:16:23
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/parking" )
@Tag(name = "停车场表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ParkingController {

    private final  ParkingService parkingService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param parking 停车场表
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('stop_parking_get')" )
    public R<IPage<ParkingVO>> getParkingPage(Page page, Parking parking) {
        return R.ok(parkingService.getParkingWithDeptPage(page, parking));
    }
	/*@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page" )
	@PreAuthorize("@pms.hasPermission('stop_parking_get')" )
	public R getParkingPage(Page page, Parking parking) {
		return R.ok(parkingService.page(page, Wrappers.query(parking)));
	}*/

	@Operation(summary = "查询所有", description = "查询所有")
	@GetMapping("/all" )
	@PreAuthorize("@pms.hasPermission('stop_parking_get')" )
	public R<List<Parking>> getAllParking(@RequestParam(defaultValue = "0") Integer isRoad) {
		QueryWrapper<Parking> wrapper = new QueryWrapper<>();
		wrapper.isNull("parent_park_id").eq("is_road", isRoad).orderBy(true,false, "update_time");
		return R.ok(parkingService.list(wrapper));
	}

    /**
     * 通过id查询停车场表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('stop_parking_get')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(parkingService.getParkingWithDept(id));
    }

    /**
     * 新增停车场表
     * @param parking 停车场表
     * @return R
     */
    @Operation(summary = "新增停车场表", description = "新增停车场表")
    @SysLog("新增停车场表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('stop_parking_add')" )
    public R save(@RequestBody Parking parking) {
		parking.setSecret(UUID.randomUUID().toString().replace("-", ""));
        return R.ok(parkingService.save(parking));
    }

    /**
     * 修改停车场表
     * @param parking 停车场表
     * @return R
     */
    @Operation(summary = "修改停车场表", description = "修改停车场表")
    @SysLog("修改停车场表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('stop_parking_edit')" )
    public R updateById(@RequestBody Parking parking) {
        return R.ok(parkingService.updateById(parking));
    }

    /**
     * 通过id删除停车场表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id删除停车场表", description = "通过id删除停车场表")
    @SysLog("通过id删除停车场表" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('stop_parking_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(parkingService.removeById(id));
    }

}
